package com.fr.chicken.service.Impl;



import com.fr.chicken.dao.*;
import com.fr.chicken.facade.base.BaseResponseDTO;
import com.fr.chicken.facade.info.FriendListInfo;
import com.fr.chicken.facade.request.*;
import com.fr.chicken.facade.response.FindFriendResponseDTO;
import com.fr.chicken.facade.response.GetFriendListResponseDTO;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.manager.friend.FriendManager;
import com.fr.chicken.manager.message.MessageManager;
import com.fr.chicken.model.*;
import com.fr.chicken.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("friendService")
public class FriendServiceImpl implements FriendService {

    @Autowired
    private ActivationLogDao activationLogDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private ChickenInfoDao chickenInfoDao;
    @Autowired
    private HouseInfoDao houseInfoDao;
    @Autowired
    private FriendInfoDao friendInfoDao;
    @Autowired
    private AccountListDao accountListDao;
    @Autowired
    private MessageLogDao messageLogDao;
    @Autowired
    private FriendManager friendManager;
    @Autowired
    private MessageManager messageManager;
    @Autowired
    private RebateLogDao rebateLogDao;
    @Autowired
    private ConfigManager configManager;
    @Override
    public GetFriendListResponseDTO getFriendList(Long playerId) {
         GetFriendListResponseDTO responseDTO =new GetFriendListResponseDTO();
         ActivationLog activationLog = activationLogDao.selectByInviteeId(playerId);
        List<FriendListInfo> friendListInfos = new ArrayList<>();
         if (activationLog!=null) {
             HouseInfo houseInfo = houseInfoDao.selectByPlayerId(playerId);
             PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(activationLog.getInviterId());
             FriendListInfo friendListInfo = new FriendListInfo();
             friendListInfo.setPlayerId(playerInfo.getId());
             friendListInfo.setFriendType(1);
             friendListInfo.setHeadPic(playerInfo.getHeadPic());
             friendListInfo.setNickName(playerInfo.getNickName());
             int number = chickenInfoDao.countByHouseId(houseInfo.getId());
             friendListInfo.setChickenNum(number);
             friendListInfos.add(friendListInfo);
         }
         List<ActivationLog> activationLogs = activationLogDao.selectByInviterId(playerId);
         ArrayList<Long> list = new ArrayList();
         for (ActivationLog x:activationLogs){
             list.add(x.getInviteeId());
         }
         if (list.size()>0) {
             List<PlayerInfo> playerInfos = playerInfoDao.selectByIdList(list);
             for (PlayerInfo x : playerInfos) {
                 FriendListInfo friendListInfo1 = new FriendListInfo();
                 friendListInfo1.setNickName(x.getNickName());
                 friendListInfo1.setFriendType(2);
                 friendListInfo1.setPlayerId(x.getId());
                 friendListInfo1.setHeadPic(x.getHeadPic());
                 List<RebateLog> rebateLogs = rebateLogDao.selectByRecipientIdAndPlayerId(playerId,x.getId());
                 Integer contribution = 0;
                 for (RebateLog r:rebateLogs){
                     contribution=contribution+r.getNum();
                 }
                 friendListInfo1.setContribution(contribution);
                 HouseInfo houseInfo1 = houseInfoDao.selectByPlayerId(x.getId());
                 Integer number1 = chickenInfoDao.countByHouseId(houseInfo1.getId());
                 friendListInfo1.setChickenNum(number1);
                 friendListInfos.add(friendListInfo1);
             }
         }
         List<Map<String,Object>> mapList = friendInfoDao.selectByPlayerId(playerId);
         ArrayList<Integer> list1 = new ArrayList();
         for (Map<String,Object> x:mapList){
               list1.add(new Integer(x.get("friends").toString()));
         }
        if (list1.size()>0) {
            List<PlayerInfo> playerInfos1 = playerInfoDao.selectByIdList(list1);
            for (PlayerInfo x : playerInfos1) {
                FriendListInfo friendListInfo1 = new FriendListInfo();
                friendListInfo1.setNickName(x.getNickName());
                friendListInfo1.setFriendType(3);
                friendListInfo1.setPlayerId(x.getId());
                friendListInfo1.setHeadPic(x.getHeadPic());
                HouseInfo houseInfo1 = houseInfoDao.selectByPlayerId(x.getId());
                Integer number1 = chickenInfoDao.countByHouseId(houseInfo1.getId());
                friendListInfo1.setChickenNum(number1);
                friendListInfos.add(friendListInfo1);
            }
        }
         responseDTO.setMsg("查询成功");
         responseDTO.success();
         responseDTO.setFriendListInfos(friendListInfos);
         return responseDTO;
    }

    @Override
    public FindFriendResponseDTO findFriendByPlayerId(FindFriendByPlayerIdRequsetDTO requsetDTO) {
        FindFriendResponseDTO responseDTO = new FindFriendResponseDTO();
        if (friendManager.isFriend(requsetDTO.getPlayerId(),requsetDTO.getFriendId())){
            responseDTO.setFriend(true);
        }else {
            responseDTO.setFriend(false);
        }
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(requsetDTO.getFriendId());
        if (playerInfo!=null){
            responseDTO.success();
            responseDTO.setMsg("查询成功");
            responseDTO.setNickName(playerInfo.getNickName());
            responseDTO.setPlayerId(playerInfo.getId());
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("不存在该用户");
        return responseDTO;
    }

    @Override
    public FindFriendResponseDTO findFriendByNickName(FindFriendByNickNameRequsetDTO requsetDTO) {
        FindFriendResponseDTO responseDTO = new FindFriendResponseDTO();
        PlayerInfo playerInfo = playerInfoDao.selectByNickName(requsetDTO.getNickName());
        if (playerInfo!=null){
        if (friendManager.isFriend(requsetDTO.getPlayerId(),playerInfo.getId())){
            responseDTO.setFriend(true);
        }else {
            responseDTO.setFriend(false);
        }
            responseDTO.setMsg("查询成功");
            responseDTO.success();
            responseDTO.setNickName(playerInfo.getNickName());
            responseDTO.setPlayerId(playerInfo.getId());
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("不存在该用户");
        return responseDTO;
    }

    @Override
    public FindFriendResponseDTO findFriendByPhoneNumber(FindFriendByPhoneRequsetDTO requsetDTO) {
        FindFriendResponseDTO responseDTO = new FindFriendResponseDTO();
        AccountList accountList =accountListDao.selectByAccount(requsetDTO.getPhoneNumber());
        if (accountList==null){
            responseDTO.success();
            responseDTO.setMsg("不存在该用户");
            return responseDTO;
        }
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(accountList.getPlayerId().longValue());
        if (friendManager.isFriend(requsetDTO.getPlayerId(),playerInfo.getId())){
            responseDTO.setFriend(true);
        }else {
            responseDTO.setFriend(false);
        }
        responseDTO.setMsg("查询成功");
        responseDTO.success();
        responseDTO.setNickName(playerInfo.getNickName());
        responseDTO.setPlayerId(playerInfo.getId());
        return responseDTO;
    }

    @Override
    public BaseResponseDTO deleteFriend(DeleteFriendRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        int num = friendInfoDao.deleteFriend(requsetDTO.getPlayerId(),requsetDTO.getFriendId());
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("删除失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("删除成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO addFriend(AddFriendRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        if (friendManager.isFriend(requsetDTO.getPlayerId(),requsetDTO.getFriendId())){
            responseDTO.faild();
            responseDTO.setMsg("你们已经是好友");
            return responseDTO;
        }
        if(messageManager.queryApplyFriend(requsetDTO.getPlayerId(),requsetDTO.getFriendId())){
            responseDTO.faild();
            responseDTO.setMsg("已经申请过了");
            return responseDTO;
        }
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
        MessageLog messageLog = new MessageLog();
        messageLog.setContent(playerInfo.getNickName()+"请求加你为好友");
        messageLog.setSenderId(requsetDTO.getPlayerId());
        messageLog.setRecipientId(requsetDTO.getFriendId());
        messageLog.setSendTime(System.currentTimeMillis() / 1000);
        messageLog.setType((byte)2);
        int num = messageLogDao.insertSelective(messageLog);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("申请失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("申请成功");
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO isAgreeAddFriend(IsAgreeAddFriendRequsetDTO requsetDTO) {
         BaseResponseDTO responseDTO = new BaseResponseDTO();
         MessageLog messageLog = messageLogDao.selectByPrimaryKey(requsetDTO.getId());
         if (friendManager.isFriend(messageLog.getSenderId(),messageLog.getRecipientId())){
            responseDTO.faild();
            responseDTO.setMsg("你们已经是好友");
            return responseDTO;
         }
         if (messageLog==null){
             responseDTO.faild();
             responseDTO.setMsg("消息不存在");
             return responseDTO;
         }
         if (messageLog.getType()!=2){
             responseDTO.faild();
             responseDTO.setMsg("不是申请添加好友消息");
             return responseDTO;
         }
         PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(messageLog.getRecipientId());
         MessageLog messageLog1 = new MessageLog();
         messageLog1.setType((byte)1);
         messageLog1.setSenderId((long)0);
         messageLog1.setRecipientId(messageLog.getSenderId());
         messageLog1.setSendTime(System.currentTimeMillis() / 1000);
         if (requsetDTO.getIsAdd()==1){
             FriendInfo friendInfo = new FriendInfo();
             friendInfo.setPlayerId(messageLog.getSenderId());
             friendInfo.setFriendId(messageLog.getRecipientId());
             int num = friendInfoDao.insertSelective(friendInfo);
             if (num<1){
                 throw new RuntimeException("添加好友失败");
             }
         messageLog.setIsReceive((byte)2);
         messageLog.setIsRead((byte)2);
         messageLog1.setContent(playerInfo.getNickName()+"同意了你的好友申请");
         }else {
             messageLog.setIsReceive((byte)3);
             messageLog.setIsRead((byte)2);
             messageLog1.setContent(playerInfo.getNickName()+"拒绝了你的好友申请");
         }
         int num1 = messageLogDao.updateByPrimaryKeySelective(messageLog);
         if (num1<1){
                 throw new RuntimeException("添加好友失败");
         }
         int num2 = messageLogDao.insertSelective(messageLog1);
        if (num2<1){
            throw new RuntimeException("添加好友失败");
        }
        responseDTO.success();
        responseDTO.setMsg("添加好友成功");
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO giveFriendEgg(GiveFriendEggRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        MessageLog messageLog = new MessageLog();
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
        PlayerInfo playerInfo1 = playerInfoDao.selectByPrimaryKey(requsetDTO.getFriendId());
        int poundage  = configManager.getIntConfig(11);
        if (playerInfo1==null||playerInfo==null){
            responseDTO.faild();
            responseDTO.setMsg("用户不存在");
            return responseDTO;
        }
        if (playerInfo.getEgg()<(requsetDTO.getNum()+(requsetDTO.getNum()*poundage)/100)){
            responseDTO.faild();
            responseDTO.setMsg("鸡蛋数量不足");
            return responseDTO;
        }
        if (requsetDTO.getNum()%10>0){
            responseDTO.faild();
            responseDTO.setMsg("鸡蛋数量不是10的倍数");
            return responseDTO;
        }
        playerInfo.setEgg(playerInfo.getEgg()-requsetDTO.getNum()-((requsetDTO.getNum()*poundage)/100));
        int num = playerInfoDao.updateByPrimaryKeySelective(playerInfo);
        if (num<1){
            throw new RuntimeException("赠送鸡蛋失败");
        }
        playerInfo1.setEgg(playerInfo1.getEgg()+requsetDTO.getNum());
        int num1 = playerInfoDao.updateByPrimaryKeySelective(playerInfo1);
        if (num1<1){
            throw new RuntimeException("赠送鸡蛋失败");
        }
        messageLog.setType((byte)3);
        messageLog.setSendTime(System.currentTimeMillis() / 1000);
        messageLog.setSenderId(requsetDTO.getPlayerId());
        messageLog.setRecipientId(requsetDTO.getFriendId());
        messageLog.setValue1(requsetDTO.getNum());
        messageLog.setContent(playerInfo.getNickName()+"向你赠送"+requsetDTO.getNum()+"个鸡蛋");
        int num2 = messageLogDao.insertSelective(messageLog);
        if (num2<1){
            throw new RuntimeException("赠送鸡蛋失败");
        }
        responseDTO.success();
        responseDTO.setMsg("赠送成功");
        return responseDTO;
    }
}
